﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormProject.Depo.Alislar;
using WindowsFormProject.Depo.AlisDetay;

namespace WindowsFormProject.Depo
{
    public partial class FrmAlislar : Form
    {
        public FrmAlislar()
        {
            InitializeComponent();
        }
        public BindingSource MySource = new BindingSource();
        public BindingSource MySourceAlisDetay = null;
        public DataSet MyDataSet = new DataSet();
        private void FrmAlislar_Load(object sender, EventArgs e)
        {
            txtFirmaAra.Select();
            GenelIslemler.BackGrounWorkerCalistir(this.BGWAlisDoldur);
            //foreach (Control control in groupBox1.Controls)
            //{
            //    control.Validated += new EventHandler(control_Validated);

            //}
        }

        private void BGWAlisDoldur_DoWork(object sender, DoWorkEventArgs e)
        {
            MyDataSet.Tables.Add(AlisProvider.AlislarByDataTable());
        }

        private void BGWAlisDoldur_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MySource.DataSource = MyDataSet;
            MySource.DataMember = MyDataSet.Tables["Alislar"].TableName;
            DGWAlislar.DataSource = MySource;
            MySource.Position = MySource.Count;

            bindingNavigator1.BindingSource = MySource;
            //GenelIslemler.TextboxBindingEt(MySource, this.groupBox1.Controls);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (MySource.Filter!=string.Empty )
            {
                MySource.RemoveFilter();  
            }
            
            FrmTedarikciler FrmTedarikciler = new FrmTedarikciler(this);
            this.AddOwnedForm(FrmTedarikciler);
            FrmTedarikciler.ShowDialog();

        }

        private void txtFirmaAra_TextChanged(object sender, EventArgs e)
        {
            if (txtFirmaAra.Text != string.Empty)
            {
                string Filtre = "TedarikciFirmaAdi Like '%" + txtFirmaAra.Text + "%'";
                MySource.Filter = Filtre;
                MySource.Position = MySource.Count - 1;
            }
            else
            {
                MySource.RemoveFilter();
                MySource.Position = MySource.Count - 1;
            }
        }
        public void AlisDetayMalzemeleriniDoldur(Int32 AlisNosu)
        {
            #region SatisDetay Tablosunu Doldurur
            MySourceAlisDetay = new BindingSource();
            
            if (this.MyDataSet.Tables["AlisDetay"] != null)
            {
                this.MyDataSet.Tables.Remove("AlisDetay");
            }

            if (this.MyDataSet.Tables["AlisDetay"] == null)
            {
                this.MyDataSet.Tables.Add(AlisDetayProvider.AlisDetayByDataTable(AlisNosu));
                MySourceAlisDetay.DataSource = this.MyDataSet ;
                MySourceAlisDetay.DataMember = this.MyDataSet.Tables["AlisDetay"].TableName;
                
                DGWAlisDetay.DataSource = MySourceAlisDetay;
                MySourceAlisDetay.Position = MySourceAlisDetay.Count;
                //DataSetTut.TextboxBindingEt(MySourceAlisDetay, this.panel6.Controls);
               


            }

            #endregion
        }

        private void DGWAlislar_CurrentCellChanged(object sender, EventArgs e)
        {
            //try
            //{
            if (DGWAlislar.CurrentRow != null)
            {
                Int32 AlisNo = Convert.ToInt32(DGWAlislar[0, DGWAlislar.CurrentRow.Index].Value);
                AlisDetayMalzemeleriniDoldur(AlisNo);
                if (DGWAlisDetay.RowCount > 0)
                {
                    foreach (DataGridViewRow Rw in DGWAlisDetay.Rows)
                    {
                        TutarHesapla(Rw);
                    }
                    KontrolBagla();
                }
            
            
            
            }
               
            //}
            //catch (Exception ex)
            //{
            //    //MessageBox.Show(ex.Message);
                
            //}
        }
        public void TutarHesapla(DataGridViewRow Row)
        {

            if (Row!=null)
            {
                
            
            //MessageBox.Show(Row.Cells[12].Value.ToString());
            decimal Adet = Convert.ToDecimal(Row.Cells[11].Value);
            decimal Fiyat = Convert.ToDecimal(Row.Cells[12].Value);
            decimal Tutar = Adet * Fiyat;
            decimal KDV = Tutar * Convert.ToDecimal(Row.Cells[13].Value) / 100;
            decimal KDVli = Tutar + KDV;
            decimal Iskonto = KDVli * Convert.ToDecimal(Row.Cells[14].Value) / 100;
            decimal Iskontolu = KDVli - Iskonto;
            Row.Cells[0].Value = Tutar;
            Row.Cells[1].Value = KDV ;
            Row.Cells[2].Value = Iskonto;
            Row.Cells[3].Value = Iskontolu;
            }
        }
        public decimal  SiparisToplamTutarHesapla(DataGridView  Source)
        {
            decimal ToplamTutar = 0;
            for (int i = 0; i < Source.RowCount ; i++)
            {
               if (Source.Rows[i].Cells[3].Value!=null)
               {
                   ToplamTutar += Convert.ToDecimal(Source.Rows[i].Cells[3].Value);
               }   
            
            }
         
            return ToplamTutar;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            TutarHesapla(DGWAlisDetay.CurrentRow);
            SiparisToplamTutarHesapla(DGWAlisDetay  );
        }

        private void DGWAlisDetay_CurrentCellChanged(object sender, EventArgs e)
        {
            KontrolBagla();
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MySource.Filter != string.Empty)
            {
                MySource.RemoveFilter();
            }

            UrunListesiFrm Urunler = new UrunListesiFrm(this);
            this.AddOwnedForm(Urunler);
            Urunler.ShowDialog();
        }
        public void KontrolBagla()
        {
            if (DGWAlisDetay.CurrentRow != null)
            {
                txtTutar.Text = DGWAlisDetay[0, DGWAlisDetay.CurrentRow.Index].FormattedValue.ToString();
                txtkdvtutar.Text = DGWAlisDetay[1, DGWAlisDetay.CurrentRow.Index].FormattedValue.ToString();
                txtiskontotutar.Text = DGWAlisDetay[2, DGWAlisDetay.CurrentRow.Index].FormattedValue.ToString();
                txtnettutar.Text = DGWAlisDetay[3, DGWAlisDetay.CurrentRow.Index].FormattedValue.ToString();
            }
            txtToplamTutar.Text = SiparisToplamTutarHesapla(DGWAlisDetay).ToString("#,###.00 TL");
        }

        private void BtnKaydet_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < MySourceAlisDetay.List.Count  ; i++)
            {
                DataRow MyDataRow = ((DataRowView)(MySourceAlisDetay[i])).Row;
            
            //DataRow MyDataRow = ((DataRowView)(MySourceAlisDetay.Current)).Row;
            
            
            #region Guncellenmişse
            
            int sonuc =AlisDetayProvider.UpdateAlisDetay(MyDataRow);
            if (sonuc > 0)
            {
                MyDataRow.AcceptChanges();
                GenelIslemler.ToolstripMesajYolla(this.MdiParent,i.ToString()+ ". Satır başarıyla güncellenmiştir...");
            }
            else
            {
                MyDataRow.RejectChanges();
                GenelIslemler.ToolstripMesajYolla(this.MdiParent, "Güncelleme işleminde hata oluştu.Güncelleme iptal edildi");
            }
            #endregion
            }
        }

        private void DGWAlisDetay_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //MessageBox.Show(e.ColumnIndex.ToString() );
            if (e.ColumnIndex == 11 || e.ColumnIndex == 12 || e.ColumnIndex == 13 || e.ColumnIndex == 14)
            {
                TutarHesapla(DGWAlisDetay.CurrentRow);
                
            }
        }
    }
}
